package net.jalbum.filters;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import net.jalbum.filterManager.FilterProperties.FilterCategory;

import se.datadosen.jalbum.Msg;

public class RedEyeFilterPlugin extends MultipleResizableCirclePanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2930029844322497620L;
	protected RedEyeControl redEyeControl;
	protected RedEyeCircle redEyeCircle;
	protected RedEyeFilter redEye;

	public RedEyeFilterPlugin() {
		this.name = Msg.getString(this, "filters.redEye.name");
		this.shortName = Msg.getString(this, "filters.redEye.shortName");
		this.icon = new ImageIcon(this.getClass().getResource("res/redeye.png"));
		this.author = "David Fichtmueller";
		this.version = "0.1 beta";
		this.description = Msg.getString(this, "filters.redEye.description");
		this.category = FilterCategory.OTHER;
		this.prescale = false;
		this.postscale = true;
		this.emptyClickCreatesNewObject = true;

		this.namePrefix = "RedEyeCircle ";

		this.redEye = new RedEyeFilter();

		this.redEyeControl = new RedEyeControl(this);

		this.multipleResizableCircleControl = this.redEyeControl;
		this.multipleResizableObjectControl = this.redEyeControl;
		this.resizableObjectControl = this.redEyeControl;

	}

	@Override
	public void init() {
		this.redEyeCircle = new RedEyeCircle();

		this.redEyeCircle.setName(this.namePrefix + this.counter);
		this.redEyeCircle.setParentPanel(this);

		this.objectVector.clear();
		this.objectVector.add(this.redEyeCircle);

		this.resizableCircle = this.redEyeCircle;
		this.resizableObject = this.redEyeCircle;
		this.hoveredOverObject = this.redEyeCircle;
		this.redEyeControl.adjustResizableObject(this.resizableObject);
	}

	@Override
	public JPanel getControls() {
		return this.redEyeControl;
	}

	@Override
	public void addObject() {
		this.resizableObject.setState(ResizableObject.NOTHING);

		this.redEyeCircle = new RedEyeCircle(this.width, this.height);
		this.redEyeCircle.setParentPanel(this);
		// new circle is added on top of the others
		this.objectVector.add(0, this.redEyeCircle);
		this.redEyeCircle.setName(this.namePrefix + this.counter);
		this.resizableCircle = this.redEyeCircle;
		this.resizableObject = this.resizableCircle;
		this.hoveredOverObject = this.redEyeCircle;
		this.counter++;
		this.redEyeControl.adjustResizableObject(this.resizableObject);
		this.redEyeControl.checkButtons();
	}

	@Override
	public boolean removeObject() {
		// remove current object, jump to top object in stack

		if (this.objectVector.size() > 1) {
			super.removeObject();
		} else {
			// replace last item with new
			final ResizableObject temp = this.resizableObject;
			this.addObject();
			this.objectVector.remove(temp);
		}

		this.redEyeControl.checkButtons();
		this.redEyeControl.adjustResizableObject(this.resizableObject);
		this.renderPreview();

		return true;
	}

	@Override
	public boolean removeObject(final int index) {
		final boolean temp = super.removeObject(index);
		this.renderPreview();
		return temp;
	}

	@Override
	public boolean removeObject(final String name) {
		final boolean temp = super.removeObject(name);
		this.renderPreview();
		return temp;
	}

	@Override
	public BufferedImage renderImage(final BufferedImage bi) {
		BufferedImage out = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		// transfer data from resizedImage
		out.setData(bi.getData());

		if (this.redEye == null) {
			this.redEye = new RedEyeFilter();
		}
		// go through vector and render image for each object in vector
		for (int i = 0; i < this.objectVector.size(); i++) {
			out = this.renderObject(this.objectVector.get(i), out);
		}
		return out;
	}

	@Override
	protected BufferedImage renderObject(final ResizableObject object, BufferedImage bi) {
		if (!object.isInitial()) {
                    this.width = bi.getWidth();
                    this.height = bi.getHeight();                    
//                    System.out.println("Using width " + width + " and height " + height);
			this.redEye.setRadius((int) (((RedEyeCircle) object).getRadius() * (this.width - 1)));
			this.redEye.setX((int) (((RedEyeCircle) object).getX1() * (this.width - 1)));
			this.redEye.setY((int) (((RedEyeCircle) object).getY1() * (this.height - 1)));
			this.redEye.setMaxGap((int) (((RedEyeCircle) object).getMaxGap() * ((RedEyeCircle) object).getX1() * (this.width - 1)));
			this.redEye.setValue1(((RedEyeCircle) object).getValue1());
			this.redEye.setValue2(((RedEyeCircle) object).getValue2());
			bi = this.redEye.filter(bi);
		}
		return bi;
	}

	@Override
	public void dispose() {
		super.dispose();
		this.redEye = null;
		this.redEyeCircle = null;
	}

	@Override
	public void mouseReleased(final MouseEvent arg0) {
		super.mouseReleased(arg0);
		this.applyFilter = !this.resizableObject.isInitial();
		this.redEyeControl.checkButtons();
		this.renderPreview();
	}

	@Override
	public void keyPressed(final KeyEvent arg0) {
		super.keyPressed(arg0);
		this.renderPreview();
	}

	public double getValue1() {
		return ((RedEyeCircle) this.resizableObject).getValue1();
	}

	public void adjustValue1(final double value1) {
		((RedEyeCircle) this.resizableObject).setValue1(value1);
		this.renderPreview();
	}

	public double getValue2() {
		return ((RedEyeCircle) this.resizableObject).getValue2();
	}

	public void adjustValue2(final double value2) {
		((RedEyeCircle) this.resizableObject).setValue2(value2);
		this.renderPreview();
	}

	public double getValueFactor() {
		return ((RedEyeCircle) this.resizableObject).getValueFactor();
	}

	public void adjustValueFactor(final double valueFactor) {
		((RedEyeCircle) this.resizableObject).setValueFactor(valueFactor);
	}

	public double getMaxGap() {
		return ((RedEyeCircle) this.resizableObject).getMaxGap();
	}

	public void adjustMaxGap(final double maxGap) {
		((RedEyeCircle) this.resizableObject).setMaxGap(maxGap);
		this.renderPreview();
	}

	public RedEyeCircle getRedEyeCircle() {
		return this.redEyeCircle;
	}

	public void setRedEyeCircle(final RedEyeCircle redEyeCircle) {
		this.redEyeCircle = redEyeCircle;
	}

	public RedEyeFilter getRedEye() {
		return this.redEye;
	}

	public void setRedEye(final RedEyeFilter redEye) {
		this.redEye = redEye;
	}
}
